package com.woniu.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author LXB
 * @Date 2023/8/22 20:38
 * @Description
 **/

public class TimeUtil {



    public static Long calculateTime(){
        // 计算出到0点之前有多少毫秒
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        Long timeout = calendar.getTimeInMillis() - System.currentTimeMillis();
        if (timeout <= 0L){
            timeout = 30000L;
        }

        return timeout;
    }

    /**
     * 获取前n天的开始时间
     * @param sub 2就是前2天
     * @return
     */
    public static String getTheOtherDayDate(Integer sub){
        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.DAY_OF_MONTH,-sub);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }
    /**
     * 当天开始时间
     * @return
     */
    public static String getNowStartDate(){
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    /**
     * 当天结束时间
     * @return
     */
    public static String getNowEndDate(){
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,59);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    /**
     * 计算两个时间相差多少分钟，不足1分钟按1分钟算
     * @param dateA
     * @param dateB
     * @return
     */
    public static long calculateMinute(Date dateA,Date dateB){
        long timeA = dateA.getTime();
        long timeB = dateB.getTime();
        long timeConsuming = timeB - timeA;
        if (timeConsuming < 60000){
            return 1L;
        }
        return  timeConsuming / 1000 / 60;

    }

    /**
     * 格式化当前时间
     * @return
     */
    public static String nowDateStr(String format){
        return new SimpleDateFormat(format).format(new Date());
    }

    /**
     * 判断是否是当天
     * @param date 对比的时间
     * @return
     */
    public static boolean isToday(Date date){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        if (dateFormat.format(date).equals(dateFormat.format(new Date()))){
            return true;
        }
        return false;
    }


}
